import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/expert/plan_form_detail_logic.dart';
import 'package:ball_lion/entity/expert/match_play_entity.dart';
import 'package:ball_lion/entity/expert/plan_form/plan_form_detail_entity.dart';
import 'package:ball_lion/entity/match/expert_views_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/expert/viewpoint/match_play_view.dart';
import 'package:ball_lion/module/widget/history_label.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/base/wqs_image.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class PlanFormDetailPage extends StatefulWidget {
  const PlanFormDetailPage({super.key});

  @override
  State<PlanFormDetailPage> createState() => _PlanFormDetailPageState();
}

class _PlanFormDetailPageState extends State<PlanFormDetailPage> {
  final PlanFormDetailLogic logic = Get.put(PlanFormDetailLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlanFormDetailLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: Colours.greyF2F3F7,
        appBar: Styles.appBar(title: const QsText("计划详情")),
        body: Container(
          decoration: const BoxDecoration(
              border:
                  Border(top: BorderSide(color: Colours.greyEE, width: 0.5))),
          child: SingleChildScrollView(
            child: Column(children: [
              Container(height: 15),
              Row(
                children: [
                  ClipOval(
                    child: QsImage(
                        imageUrl: logic.data?.expertLogo,
                        width: 40,
                        height: 40),
                  ),
                  Container(width: 8),
                  QsText(logic.data?.expertName ?? "",
                      fontWeight: FontWeight.w500)
                  // Container(width: 5),
                  // if(logic.issueInfo?.buy.hasValue == true)
                  // SizedBox(
                  //   height: 17,
                  //   child: Stack(
                  //     alignment: AlignmentDirectional.center,
                  //     children: [
                  //       Positioned.fill(
                  //         child: Image.asset(Utils.getImgPath("plan_buy_tag.png"),fit: BoxFit.fill)
                  //       ),
                  //       QsText("${logic.issueInfo?.buy}人已购买",
                  //         strutStyle: Styles.centerStyle(fontSize: 12),
                  //         style: const TextStyle(fontSize: 12,color: Colours.white)
                  //       ).paddingOnly(left: 13,right: 4),
                  //     ],
                  //   ),
                  // )
                ],
              ).paddingSymmetric(horizontal: 16),
              Container(height: 18),
              if (logic.issueInfo?.state == 2 || logic.issueInfo?.state == 3)
                profitLabel().paddingSymmetric(horizontal: 16),
              Column(
                children: logic.issueInfo?.issueDetail
                        ?.map((e) => stage(e))
                        .toList() ??
                    [],
              )
            ]),
          ),
        ),
      );
    });
  }

  Widget stage(IssueDetail entity) {
    return Block(
      cardStyle: const CardStyle(
          margin: EdgeInsets.fromLTRB(16, 8, 16, 0),
          color: Colours.white,
          radius: 10,
          shadow: BoxShadow(color: Colors.transparent)),
      child: Stack(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.fromLTRB(16, 10, 0, 10),
                child: QsText("阶段${entity.stage.toChn}"),
              ),
              const Divider(color: Colours.greyEE, height: 0.5)
                  .paddingSymmetric(horizontal: 16),
              Container(height: 10),
              ...List<Widget>.generate(entity.matchBries?.length ?? 0, (index) {
                var match = entity.matchBries?[index];
                var itemList = entity.itemShow
                    ?.where((element) => element.matchId == match?.matchId)
                    .toList();
                return item(MatchPlayEntity.fromPlanForm(entity), match);
              })
            ],
          ),
          Positioned(
              top: 16,
              right: 16,
              child: HistoryLabel.planFormDetail(state: entity.state))
        ],
      ),
    );
  }

  Widget item(MatchPlayEntity? entity, MatchBries? match) {
    return MatchPlayView(
      match: match,
      matchPlay: entity,
    );
  }

  Widget profitLabel() {
    return SizedBox(
      height: 36,
      child: Stack(alignment: AlignmentDirectional.center, children: [
        Container(
            height: 32,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: Colours.white, borderRadius: BorderRadius.circular(4)),
            child: DefaultTextStyle(
              style: const TextStyle(fontSize: 12, color: Colours.text),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (logic.issueInfo?.state == 2) ...[
                    QsText("本期综合回报率 ", needCenter: true),
                    QsText(
                        "${((logic.issueInfo?.issueProfit ?? 0) * 100).toInt()}%",
                        color: Colours.main)
                  ] else ...[
                    QsText("本期未命中")
                  ]
                ],
              ),
            )),
        Positioned(
            right: 32,
            child: HistoryLabel.planForm(state: logic.issueInfo?.state))
      ]),
    );
  }
}
